/*
 * @Author: hongbin
 * @Date: 2021-11-09 20:50:24
 * @LastEditors: hongbin
 * @LastEditTime: 2021-11-18 21:15:32
 * @Description: MongoDB相关请求
 */

import { request, query } from ".";
import {
  Collections,
  ConnectParams,
  DataBaseType,
  DBS,
  RequestBack,
  uuid,
} from "./types";

interface ConnectBack {
  id: uuid;
  code: number;
  dbs: DBS;
  message: string;
  databaseName: string; //当前连接的数据库名
}

export const connect = (params: ConnectParams): RequestBack<ConnectBack> =>
  request("/createConnect_Mongo", params);

interface GetCollectionsParams {
  databaseName: string; //哪个数据库
  id: uuid; //服务端连接的id
  type: DataBaseType; //数据库类型 mongo  mysql sqlServer
}

export const getCollections = (
  params: GetCollectionsParams
): RequestBack<{ code: number; collections: Collections }> =>
  query("/getCollections", { params });

interface ToggleDatabaseParams {
  id: uuid;
  databaseName: string;
  type: DataBaseType;
}

export const toggleDatabase = (params: ToggleDatabaseParams) =>
  request("/toggleDatabase", params);

interface QueryCollectionParams {
  collection: string;
  type: DataBaseType;
  id: string;
}

export const queryCollection = (params: QueryCollectionParams) =>
  query("/queryCollection", { params });

interface DeleteDocumentProps {
  type: DataBaseType;
  clientId: uuid; //TODO id不利于理解 改用 clientId
  documentId: string;
  collection: string;
}
export const deleteDocument = (params: DeleteDocumentProps) =>
  request("/deleteDocument", params);

interface UpdateDocumentProps extends DeleteDocumentProps {
  changeField: { [key: string]: any };
  removeField: string[];
}
export const updateDocument = (params: UpdateDocumentProps) =>
  request("/updateDocument", params);
